home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Magazin/MacEasy 13
/
Mac Magazin and MacEasy Magazine CD - Issue 13.iso
/
Wissenschaft & Technik
/
ResAnomaly dist
/
ResAnomaly Source
/
LTempHandleStream.cp
< prev
next >
Wrap
Text File
|
1995-08-04
|
3KB
|
152 lines
// ===========================================================================
// LHandleStream.cp ©1993 Metrowerks Inc. All rights reserved.
// ===========================================================================
//
// A Stream whose bytes are in a Handle block in memory
// * Note: ckt modified to use temp. memory! Don't know why there isn't
// * such a variant included with PP.
#ifdef PowerPlant_PCH
#include PowerPlant_PCH
#endif
#include <LHandleStream.h>
// ---------------------------------------------------------------------------
// • LHandleStream
// ---------------------------------------------------------------------------
// Default Constructor
LHandleStream::LHandleStream()
{
OSErr theErr;
mDataH = ::TempNewHandle(0, &theErr); // Start with a zero length Handle
// * purposely ignoring theErr
ThrowIfMemFail_(mDataH);
}
// ---------------------------------------------------------------------------
// • LHandleStream(Handle)
// ---------------------------------------------------------------------------
// Construct from an existing Handle
//
// The LHandleStream object assumes control of the Handle
LHandleStream::LHandleStream(
Handle inHandle)
{
mDataH = inHandle;
LStream::SetLength(GetHandleSize(inHandle));
}
// ---------------------------------------------------------------------------
// • ~LHandleStream
// ---------------------------------------------------------------------------
// Destructor
LHandleStream::~LHandleStream()
{
if (mDataH != nil) {
::DisposeHandle(mDataH);
}
}
void
LHandleStream::SetLength(
Int32 inLength)
{
::SetHandleSize(mDataH, inLength);
ThrowIfMemError_();
LStream::SetLength(inLength);
}
Int32
LHandleStream::WriteData(
const void *inBuffer,
Int32 inByteCount)
{
Int32 bytesWritten = inByteCount;
Int32 endOfWrite = GetMarker() + inByteCount;
if (endOfWrite > GetLength()) {
// Need to grow Handle
Try_ {
SetLength(endOfWrite);
}
Catch_(inErr) { // Grow failed. Write only what fits.
bytesWritten = GetLength() - GetMarker();
} EndCatch_
}
// Copy bytes into Handle
::BlockMoveData(inBuffer, *mDataH + GetMarker(), bytesWritten);
SetMarker(bytesWritten, streamFrom_Marker);
return bytesWritten;
}
Int32
LHandleStream::ReadData(
void *outBuffer,
Int32 inByteCount)
{
// Upper bound is number of bytes from
// marker to end
Int32 bytesRead = inByteCount;
if ((GetMarker() + bytesRead) > GetLength()) {
bytesRead = GetLength() - GetMarker();
}
// Copy bytes from Handle into buffer
::BlockMoveData(*mDataH + GetMarker(), outBuffer, bytesRead);
SetMarker(bytesRead, streamFrom_Marker);
return bytesRead;
}
void
LHandleStream::SetDataHandle(
Handle inHandle)
{
if (mDataH != nil) {
::DisposeHandle(mDataH);
}
mDataH = inHandle;
Int32 newLength = 0;
if (inHandle != nil) {
newLength = GetHandleSize(inHandle);
}
LStream::SetLength(newLength);
SetMarker(0, streamFrom_Start);
}
Handle
LHandleStream::GetDataHandle()
{
return mDataH;
}
Handle
LHandleStream::DetachDataHandle()
{
Handle dataHandle = mDataH; // Save current data Handle
LStream::SetLength(0);
mDataH = ::NewHandle(0); // Reset to zero-sized Handle
ThrowIfMemFail_(mDataH);
return dataHandle;
}